package cn.yyh.commons.service.impl;

import cn.yyh.commons.mapper.MyBaseMapper;
import cn.yyh.commons.service.MyBaseService;
import cn.yyh.commons.utils.BaseQuery;
import cn.yyh.commons.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: BaseServiceImpl
 * @Package: cn.yyh.basic.service.impl
 * @date 2021/6/24
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class MyBaseServiceImpl<M extends MyBaseMapper<T>,T> implements MyBaseService<T> {

    @Autowired
    public M baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        try {
            baseMapper.save(t);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public List<T> listAll() {
        return baseMapper.listAll();
    }

    @Override
    public T getById(Long id) {
        return baseMapper.getById(id);
    }

    @Override
    public PageInfo<T> pageQuery(BaseQuery query) {
        // 条件查询,获取总条数
        Integer totals = baseMapper.countByQuery(query);
        // 条件查询,获取数据集合
        List<T> list = baseMapper.pageQuery(query);
        return new PageInfo<T>(totals,list);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        baseMapper.deleteBatch(ids);
    }

    @Override
    public Boolean updateBatch(List<Long> ids) {
        return baseMapper.updateBatch(ids);
    }
}
